package com.example.game;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


import android.app.Fragment;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import static com.example.game.GamePage.player;


public class setArticleFragment extends Fragment {
    Button bt;
    setToolFragment ToolFragment;
    private ImageView imageView = null;
    TextView textView;
    boolean flag = false;
    private String URLs = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2962114696,849605379&fm=26&gp=0.jpg";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.article_display, container, false);
        bt = (Button)view.findViewById(R.id.article_back_Tool);
        textView = (TextView)view.findViewById(R.id.Article_TextView);
        textView.setText("可以使用营销号文案生成器获得你喜欢的文章哦！");
        while (flag == false)
        {
            textView.setText("可以使用营销号文案生成器获得你喜欢的文章哦！");
            Uri uri = Uri.parse("https://www.maorx.cn/yxh/");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
            flag =true;
        }
        textView.setText("这篇文章你喜欢吗?你获得了20%的营销号流量!");
        flag = false;
        player.cash *=1.2;



        bt.setOnClickListener((new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GamePage act = (GamePage) getActivity();
                FragmentTransaction transaction = act.fm.beginTransaction();
                ToolFragment = new setToolFragment();
                transaction.replace(R.id.Fragment, ToolFragment);
                transaction.commit();
                Log.d("article", "onClick: checkpoint");
            }
        }));
        imageView = (ImageView)view.findViewById(R.id.article_image);
        ImageDownloadTask task = new ImageDownloadTask();
        task.execute(URLs);
        return view;
    }



    class ImageDownloadTask extends AsyncTask<String, Void, Bitmap>
    {


        @Override
        protected Bitmap doInBackground(String... params) {
            Bitmap bitmap = null;
            String url = params[0];
            URLConnection connection;
            InputStream is;
            try{
                connection = new URL(url).openConnection();
                is = connection.getInputStream();    //inputStream
                BufferedInputStream buf = new BufferedInputStream(is);
                bitmap = BitmapFactory.decodeStream(buf); //送入bitmap工厂
                is.close();
                buf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap;
        }
        @Override
        protected void onPostExecute(Bitmap result)
        {
            imageView.setImageBitmap(result);
        }


    }
}

